package com.knowledge.business.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.knowledge.business.domain.TPushDevice;
import com.knowledge.business.domain.TPushQuestion;
import com.knowledge.business.service.ITPushDeviceService;
import com.knowledge.business.service.ITPushQuestionService;
import com.knowledge.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.knowledge.business.mapper.TPushSubMapper;
import com.knowledge.business.domain.TPushSub;
import com.knowledge.business.service.ITPushSubService;

/**
 * 主题推送Service业务层处理
 *
 * @author zhaoyk
 * @date 2025-11-24
 */
@Service
public class TPushSubServiceImpl extends ServiceImpl<TPushSubMapper, TPushSub> implements ITPushSubService {
    @Autowired
    private TPushSubMapper tPushSubMapper;


    @Autowired
    private ITPushQuestionService pushQuestionService;


    @Autowired
    private ITPushDeviceService pushDeviceService;

    /**
     * 查询主题推送
     *
     * @param id 主题推送主键
     * @return 主题推送
     */
    @Override
    public TPushSub selectTPushSubById(String id) {
        TPushSub sub = tPushSubMapper.selectTPushSubById(id);
        TPushDevice tPushDevice = new TPushDevice();
        tPushDevice.setPushSubId(id);
        List<TPushDevice> tPushDevices = pushDeviceService.selectTPushDeviceList(tPushDevice);
        sub.setDeviceList(tPushDevices);
        // 问题信息
        TPushQuestion tPushQuestion = new TPushQuestion();
        tPushQuestion.setPushSubId(id);
        List<TPushQuestion> tPushQuestions = pushQuestionService.selectTPushQuestionList(tPushQuestion);
        sub.setQuestionList(tPushQuestions);
        return sub;
    }

    /**
     * 查询主题推送列表
     *
     * @param tPushSub 主题推送
     * @return 主题推送
     */
    @Override
    public List<TPushSub> selectTPushSubList(TPushSub tPushSub) {
        List<TPushSub> tPushSubs = tPushSubMapper.selectTPushSubList(tPushSub);
        for (int i = 0; i < tPushSubs.size(); i++) {
            String id = tPushSubs.get(i).getId();
            TPushDevice tPushDevice = new TPushDevice();
            tPushDevice.setPushSubId(id);
            List<TPushDevice> tPushDevices = pushDeviceService.selectTPushDeviceList(tPushDevice);
            tPushSubs.get(i).setDeviceList(tPushDevices);
            // 问题信息
            TPushQuestion tPushQuestion = new TPushQuestion();
            tPushQuestion.setPushSubId(id);
            List<TPushQuestion> tPushQuestions = pushQuestionService.selectTPushQuestionList(tPushQuestion);
            tPushSubs.get(i).setQuestionList(tPushQuestions);
        }
        return tPushSubs;
    }

    /**
     * 新增主题推送
     *
     * @param tPushSub 主题推送
     * @return 结果
     */
    @Override
    public int insertTPushSub(TPushSub tPushSub) {
        // 设备Id集合
        List<TPushDevice> deviceList = tPushSub.getDeviceList();
        // 问题集合
        List<TPushQuestion> questionList = tPushSub.getQuestionList();
        //保存主题推送主体信息
        tPushSub.setCreateTime(DateUtils.getNowDate());
        this.save(tPushSub);
        // 如果主体信息保存成功，则保存关联的设备和问题信息

        String subId = tPushSub.getId(); // 获取插入后的主键ID

        // 批量保存设备信息
        if (deviceList != null && !deviceList.isEmpty()) {
            deviceList.forEach(device -> {
                device.setPushSubId(subId); // 设置外键关联
                pushDeviceService.save(device); // 调用IService的save方法
            });
        }
        // 批量保存问题信息
        if (questionList != null && !questionList.isEmpty()) {
            questionList.forEach(question -> {
                question.setPushSubId(subId); // 设置外键关联
                pushQuestionService.save(question); // 调用IService的save方法
            });
        }


        return 1;
    }

    /**
     * 修改主题推送
     *
     * @param tPushSub 主题推送
     * @return 结果
     */
    @Override
    public int updateTPushSub(TPushSub tPushSub) {
        // 设备Id集合
        List<TPushDevice> deviceList = tPushSub.getDeviceList();
        // 问题集合
        List<TPushQuestion> questionList = tPushSub.getQuestionList();

        // 更新主题推送主体信息
        tPushSub.setUpdateTime(DateUtils.getNowDate());
        int result = tPushSubMapper.updateTPushSub(tPushSub);

        // 如果主体信息更新成功，则更新关联的设备和问题信息
        if (result > 0) {
            String subId = tPushSub.getId(); // 获取主题推送ID

            // 先删除原有的设备和问题关联数据
            pushDeviceService.remove(new LambdaQueryWrapper<TPushDevice>().eq(TPushDevice::getPushSubId, subId));
            pushQuestionService.remove(new LambdaQueryWrapper<TPushQuestion>().eq(TPushQuestion::getPushSubId, subId));

            // 重新插入新的设备信息
            if (deviceList != null && !deviceList.isEmpty()) {
                deviceList.forEach(device -> {
                    device.setPushSubId(subId); // 设置外键关联
                    pushDeviceService.save(device); // 保存设备信息
                });
            }

            // 重新插入新的问题信息
            if (questionList != null && !questionList.isEmpty()) {
                questionList.forEach(question -> {
                    question.setPushSubId(subId); // 设置外键关联
                    pushQuestionService.save(question); // 保存问题信息
                });
            }
        }

        return result;
    }

    /**
     * 批量删除主题推送
     *
     * @param ids 需要删除的主题推送主键
     * @return 结果
     */
    @Override
    public int deleteTPushSubByIds(String[] ids) {
        for (int i = 0; i < ids.length; i++) {
            pushDeviceService.remove(new LambdaQueryWrapper<TPushDevice>().eq(TPushDevice::getPushSubId, ids[i]));
            pushQuestionService.remove(new LambdaQueryWrapper<TPushQuestion>().eq(TPushQuestion::getPushSubId, ids[i]));
        }
        int i = tPushSubMapper.deleteTPushSubByIds(ids);
        return i;
    }

    /**
     * 删除主题推送信息
     *
     * @param id 主题推送主键
     * @return 结果
     */
    @Override
    public int deleteTPushSubById(String id) {
        return tPushSubMapper.deleteTPushSubById(id);
    }
}
